package com.zmn.oms.api.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.biz.mds.common.dto.VoiceDetectionDTO;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.model.entity.mediadetection.MediaDetectionResult;
import com.zmn.oms.services.interfaces.mediadetection.MediaDetectionResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 类描述：媒体质检结果监听
 *
 * @author 李刚
 * @date 2022/07/13 16:53
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
@Slf4j
public class MediaDetectionListener {

    @Resource
    private MediaDetectionResultService mediaDetectionResultService;

    @AliyunMQHandler(tags = MessageQueueTagConsts.ZMN_TAG_MEDIA_DETECTION_RESULT_NOTICE)
    public void notice(String message) {
        try {
            log.info("#MediaDetectionListener#notice message={}", message);
            VoiceDetectionDTO voiceDetectionDTO = JSON.parseObject(message, VoiceDetectionDTO.class);
            this.updateResult(voiceDetectionDTO);
        } catch (Exception e) {
            log.error("#MediaDetectionListener#notice 处理失败:{}", message, e);
        }
    }

    /**
     * 更新质检结果
     *
     * @param voiceDetectionDTO
     */
    private void updateResult(VoiceDetectionDTO voiceDetectionDTO) {
        MediaDetectionResult mediaDetectionResult = new MediaDetectionResult();
        mediaDetectionResult.setBizRecordId(Long.valueOf(voiceDetectionDTO.getBizRecordId()));
        mediaDetectionResult.setDetectRecordId(voiceDetectionDTO.getDetectRecordId());
        mediaDetectionResult.setBizId(Long.valueOf(voiceDetectionDTO.getAppBizNo()));
        mediaDetectionResult.setDetectResult(voiceDetectionDTO.getDetectResult());
        mediaDetectionResult.setCreater("系统");
        mediaDetectionResult.setUpdater("系统");
        mediaDetectionResultService.updateResultByBizRecordId(mediaDetectionResult);
    }
}
